﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using zjx.Wpf.Graffitiboard.Core;

namespace zjx.Wpf.Graffitiboard.Thumbs
{
    public class DragThumb : Thumb
    {
        private Graffitiboard board = null;

        private ISelectable item = null;

        public DragThumb()
        {
            this.DragStarted += this.DragThumb_DragStarted;
            this.DragDelta += this.DragThumb_DragDelta;
        }

        private void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            this.item = this.DataContext as ISelectable;
            if (this.item != null && this.item.IsSelected)
            {
                this.board = this.item.SelectionAdorner.AdornedElement as Graffitiboard;
            }
        }

        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ISelectable item = this.DataContext as ISelectable;
            if (this.board != null && this.item != null && this.item.IsSelected)
            {
                var designerItems = board.SelectionService.CurrentSelection.OfType<ISelectable>();
                double deltaHorizontal = e.HorizontalChange;
                double deltaVertical = e.VerticalChange;
                foreach (ISelectable designerItem in designerItems)
                {
                    designerItem.MoveTo(deltaHorizontal, deltaVertical);
                    designerItem.SelectionAdorner.InvalidateMeasure();
                }

                e.Handled = true;
            }
        }
    }
}
